//
//  SettingsViewController.m
//  ClassRoom
//
//  Created by apple on 12-12-1.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SettingsViewController.h"


#import "SettingsPopoverViewController.h"
@interface SettingsViewController ()

@end

@implementation SettingsViewController
@synthesize qqtextField;
@synthesize telephonetextField;
@synthesize settingsDic;
@synthesize attentionButton;
@synthesize areaButton;
@synthesize attentionArray;
@synthesize areaArray;

@synthesize attentionImageView;
@synthesize areaImageView;
@synthesize qqImageView;
@synthesize phoneImageView;
@synthesize commitButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    areaArray=[[NSMutableArray alloc]init];
    attentionArray=[[NSMutableArray alloc]init];
        
    settingsDic=[[NSMutableDictionary alloc]init];
    NSMutableArray* coursesType=[[NSMutableArray alloc]initWithObjects:@"四级",@"六级", @"出国",nil];
    NSMutableArray* coursesArea=[[NSMutableArray alloc]initWithObjects:@"朝阳",@"海淀",nil];
    NSLog(@"coursesArea=%@",coursesArea);
    [settingsDic setObject:coursesType forKey:@"attentionArray"];
    [settingsDic setObject:coursesArea forKey:@"areaArray"];
    [self setSettingsView];
    

}

- (void)viewDidUnload
{
    [self setQqtextField:nil];
    [self setTelephonetextField:nil];
    [self setAttentionButton:nil];
    [self setAreaButton:nil];
    [self setCommitButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)setSettingsView
{
    attentionImageView=[[UIImageView alloc]initWithFrame:CGRectMake(25, 105, 235, 42)];
    attentionImageView.image=[UIImage imageNamed:@"setting_text_background" ];
    areaImageView=[[UIImageView alloc]initWithFrame:CGRectMake(25, 155, 235, 42)];
    areaImageView.image=[UIImage imageNamed:@"setting_text_background" ];
    qqImageView=[[UIImageView alloc]initWithFrame:CGRectMake(25, 205, 235, 42)];

    qqImageView.image=[UIImage imageNamed:@"setting_text_background" ];
    phoneImageView=[[UIImageView alloc]initWithFrame:CGRectMake(25, 255, 235, 42)];

    phoneImageView.image=[UIImage imageNamed:@"setting_text_background" ];
    [self.view insertSubview:attentionImageView atIndex:0];
    //[self.view addSubview:attentionImageView];
    [self.view insertSubview:areaImageView atIndex:0];
    [self.view insertSubview:qqImageView atIndex:0];
    [self.view insertSubview:phoneImageView atIndex:0];
    
  
    attentionButton.tag=10;
    areaButton.tag=20;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


-(IBAction)backgroundTap:(id)sender//点击背景
{
    [qqtextField resignFirstResponder];
    [telephonetextField resignFirstResponder];
    //[emailTextField resignFirstResponder];
    //[nameTextField resignFirstResponder];
    //[passwordTextField resignFirstResponder];
    //[repasswordTextField resignFirstResponder];
    NSTimeInterval animationDuration = 0.30f;       
    
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];       
    
    [UIView setAnimationDuration:animationDuration];       
    
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);       
    
    self.view.frame = rect;
}

- (IBAction)popover:(UIButton *)sender {
    SettingsPopoverViewController *controller = [[SettingsPopoverViewController alloc]init];
    
    controller.choosItem=[[NSMutableArray alloc]init];
    controller.button=sender;
    controller.settingsViewController=self;
    

//    if(sender.tag==10)
//    {
//        NSLog(@"选择了类型");
//        controller.choosItem=(NSMutableArray*)[searchConditions objectForKey:@"coursesType"];
//    }
//    else if([sender.titleLabel.text isEqualToString:@"价格"])
//    {
//        
//        controller.choosItem=(NSMutableArray*)[searchConditions objectForKey:@"coursesPrice"];
//    }
//    else if([sender.titleLabel.text isEqualToString:@"地区"])
//    {
//        controller.choosItem=(NSMutableArray*)[searchConditions objectForKey:@"coursesArea"];
//    }
    
    
//    FPPopoverController *popover = [[FPPopoverController alloc] initWithViewController:controller];
//    
//    controller.popoverController=[[FPPopoverController alloc]init];
//    
//    controller.popoverController=popover;
    [controller release];
    
    //popover.arrowDirection = FPPopoverArrowDirectionAny;
    //popover.tint = FPPopoverDefaultTint;
//    popover.tint=FPPopoverLightGrayTint;
//    
//    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
//    {
//        popover.contentSize = CGSizeMake(300, 500);
//    }
//    popover.arrowDirection = FPPopoverArrowDirectionAny;
//    
//    
//    //sender is the UIButton view
//    [popover presentPopoverFromView:sender]; 
    
}

- (IBAction)commitButtonPressed:(UIButton *)sender {
}

- (void)dealloc {
    [qqtextField release];
    [telephonetextField release];
    [attentionButton release];
    [areaButton release];
    [commitButton release];
    [super dealloc];
}



#pragma mark -

#pragma mark 解决虚拟键盘挡住UITextField的方法
- (void)keyboardWillShow:(NSNotification *)noti

{       
    
    //键盘输入的界面调整       
    
    //键盘的高度
    
    float height = 216.0;               
    
    CGRect frame = self.view.frame;       
    
    frame.size = CGSizeMake(frame.size.width, frame.size.height - height);       
    
    [UIView beginAnimations:@"Curl"context:nil];//动画开始         
    
    [UIView setAnimationDuration:0.30];          
    
    [UIView setAnimationDelegate:self];         
    
    [self.view setFrame:frame];        
    
    [UIView commitAnimations];
    
}



-(BOOL)textFieldShouldReturn:(UITextField *)textField

{       
    
    // When the user presses return, take focus away from the text field so that the keyboard is dismissed.       
    
    NSTimeInterval animationDuration = 0.30f;       
    
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];       
    
    [UIView setAnimationDuration:animationDuration];       
    
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);  
    
    //CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);
    
    self.view.frame = rect;
    
    [UIView commitAnimations];
    
    [textField resignFirstResponder];
    
    return YES;       
    
}



- (void)textFieldDidBeginEditing:(UITextField *)textField

{       
    
    CGRect frame = textField.frame;
    
    int offset = frame.origin.y + 32+75 - (self.view.frame.size.height - 216.0);//键盘高度216
    
    NSTimeInterval animationDuration = 0.30f;               
    
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];               
    
    [UIView setAnimationDuration:animationDuration];
    
    float width = self.view.frame.size.width;               
    
    float height = self.view.frame.size.height;       
    
    if(offset > 0)
        
    {
        
        CGRect rect = CGRectMake(0.0f, -offset,width,height);                       
        self.view.frame = rect;       
        
    }       
    
    [UIView commitAnimations];               
    
}


@end
